package com.eschool.beans;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import com.eschool.beans.datamodels.StudentDataModel;
import com.eschool.dao.StudentDao;
import com.eschool.model.Student;
import com.eschool.service.StudentService;
import com.eschool.service.impl.StudentServiceImpl;
import com.eschool.util.UserUtil;

@ManagedBean(name="studentStatusUpdateBean")
@ViewScoped
public class StudentsStatusUpdateBean {
	
	private Student[] selectedStudentsList;
    private StudentDataModel studentsModel;
	StudentService service = new StudentServiceImpl();	
	private static List<Student> students;	
	private boolean newStatus;

	public boolean isNewStatus() {
		return newStatus;
	}

	public void setNewStatus(boolean newStatus) {
		this.newStatus = newStatus;
	}

	public StudentsStatusUpdateBean() {
		students = new ArrayList<Student>();
		
		//Map  conditions = ConstraintsUtil.getConstraintsMap();
		
		//System.out.println(UserUtil.currentUser().getName() + "   "  + UserUtil.currentUser().getBranch());
		
		//students = StudentDao.getStudentsByBranch(UserUtil.currentUser().getBranch());

		students = StudentDao.getStudents(UserUtil.currentUser().getBranch().getBranchId());
		
		studentsModel = new StudentDataModel(students);
	}

	public Student[] getSelectedStudentsList() {
		return selectedStudentsList;
	}

	public void setSelectedStudentsList(Student[] selectedStudentsList) {
		this.selectedStudentsList = selectedStudentsList;
	}

	public StudentDataModel getStudentsModel() {
		return studentsModel;
	}

	public void setStudentsModel(StudentDataModel studentsModel) {
		this.studentsModel = studentsModel;
	}
	
    public String perform(){
    	
    	List selectedStudents = Arrays.asList(selectedStudentsList);

    	service.updateStudentStatus(selectedStudents, newStatus);
    	
    	FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(
    					FacesMessage.SEVERITY_INFO,"Info message", "Student Status Updated"));
    	
    	return "studentStatusUpdate";
    }
}
